<template>
  <div class="login clearfix">
    <div class="login-wrap">
      <el-row type="flex" justify="center">
        <el-form ref="loginForm" :model="user" status-icon label-width="80px">
          <h3>注册</h3>
          <hr>
          <el-form-item prop="username" label="用户名">
            <el-input v-model="user.username" placeholder="请输入用户名" @blur="inspectUsername()"></el-input>
          </el-form-item>
          <el-form-item prop="email" label="邮箱">
            <el-input v-model="user.email" placeholder="请输入邮箱"></el-input>
          </el-form-item>
          <el-form-item prop="password" label="设置密码">
            <el-input v-model="user.password" show-password placeholder="请输入密码"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon @click="doRegister()">注册账号</el-button>
            <router-link to="/"  style="margin-left: 10px; color: blue;font-size: 20px" tag="span" > 去登录</router-link>
          </el-form-item>
        </el-form>
      </el-row>
    </div>
  </div>
</template>
 
<script>
import axios from "axios";
export default {
  name: "login",
  data() {
    return {
      user: {
        username: "",
        email: "",
        password: ""
      },
      inspectUserName:false,
    };
  },
  created() {
    // console.log($);
    // console.log("1111");
  },
  methods: {
    doRegister() {
      if (!this.user.username) {
        this.$message.error("请输入用户名！");
        return;
      } else if (!this.user.email) {
        this.$message.error("请输入邮箱！");
        return;
      } else if (this.user.email != null) {
        var reg = /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
        if (!reg.test(this.user.email)) {
          this.$message.error("请输入有效的邮箱！");
        } else if (!this.user.password) {
          this.$message.error("请输入密码！");
          return;
        } else if(!this.inspectUserName){
          this.$message.error("用户名重复！");
        }
        else {
          this.axios.post("http://localhost:8080/users/register",{
            email: this.user.email,
            account: this.user.username,
            password: this.user.password
          }).then( (res) =>{

            if (res.data.code=="500"){
              this.$message.error(res.data.msg);
            }else {
              this.$message.success(res.data.msg);
            }
          })
        }
      }
    },
    inspectUsername(){
      if (this.user.username != ''){
        this.axios.post("http://localhost:8080/users/inspect","username="+this.user.username).then( (res) =>{
          if (res.data.code=="500"){
            this.$message.error("用户名重复");
          }else {
            this.inspectUserName =true;
          }

        })

      }
    }
  }
};
</script>
 
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.login {
  width: 100%;
  height: 100vh;
  background: url("../assets/login_bg.jpg") no-repeat;
  background-size: cover;
  overflow: hidden;
}
.login-wrap {
  background: url("../assets/login_bg2.jpg") no-repeat;
  background-size: cover;
  width: 450px;
  height: 400px;
  margin: 155px auto;
  overflow: hidden;
  padding-top: 10px;
  line-height: 40px;
}
 
h3 {
  color: #0babeab8;
  font-size: 24px;
}
hr {
  background-color: #444;
  margin: 20px auto;
}
 
.el-button {
  width: 80%;
  margin-left: -50px;
}
</style>